#include "quakedef.h"
#include "image.h"
#include "jpeg.h"
#include "image_png.h"

cvar_t gl_max_size = {CVAR_SAVE, "gl_max_size", "2048", "maximum allowed texture size, can be used to reduce video memory usage, note: this is automatically reduced to match video card capabilities (such as 256 on 3Dfx cards before Voodoo4/5)"};
cvar_t gl_picmip = {CVAR_SAVE, "gl_picmip", "0", "reduces resolution of textures by powers of 2, for example 1 will halve width/height, reducing texture memory usage by 75%"};
cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1", "bilinear filters images when scaling them up to power of 2 size (mode 1), looks better than glquake (mode 0)"};
cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1", "0 = never upload textures until used, 1 = upload most textures before use (exceptions: rarely used skin colormap layers), 2 = upload all textures before use (can increase texture memory usage significantly)"};
cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1", "anisotropic filtering quality (if supported by hardware), 1 sample (no anisotropy) and 8 sample (8 tap anisotropy) are recommended values"};
cvar_t gl_texturecompression = {CVAR_SAVE, "gl_texturecompression", "0", "whether to compress textures, a value of 0 disables compression (even if the individual cvars are 1), 1 enables fast (low quality) compression at startup, 2 enables slow (high quality) compression at startup"};
cvar_t gl_texturecompression_color = {CVAR_SAVE, "gl_texturecompression_color", "1", "whether to compress colormap (diffuse) textures"};
cvar_t gl_texturecompression_normal = {CVAR_SAVE, "gl_texturecompression_normal", "0", "whether to compress normalmap (normalmap) textures"};
cvar_t gl_texturecompression_gloss = {CVAR_SAVE, "gl_texturecompression_gloss", "1", "whether to compress glossmap (specular) textures"};
cvar_t gl_texturecompression_glow = {CVAR_SAVE, "gl_texturecompression_glow", "1", "whether to compress glowmap (luma) textures"};
cvar_t gl_texturecompression_2d = {CVAR_SAVE, "gl_texturecompression_2d", "0", "whether to compress 2d (hud/menu) textures other than the font"};
cvar_t gl_texturecompression_q3bsplightmaps = {CVAR_SAVE, "gl_texturecompression_q3bsplightmaps", "0", "whether to compress lightmaps in q3bsp format levels"};
cvar_t gl_texturecompression_q3bspdeluxemaps = {CVAR_SAVE, "gl_texturecompression_q3bspdeluxemaps", "0", "whether to compress deluxemaps in q3bsp format levels (only levels compiled with q3map2 -deluxe have these)"};
cvar_t gl_texturecompression_sky = {CVAR_SAVE, "gl_texturecompression_sky", "0", "whether to compress sky textures"};
cvar_t gl_texturecompression_lightcubemaps = {CVAR_SAVE, "gl_texturecompression_lightcubemaps", "1", "whether to compress light cubemaps (spotlights and other light projection images)"};

int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
int gl_filter_mag = GL_LINEAR;

static mempool_t* texturemempool;

#define GLTEXF_UPLOAD 0x00010000
#define GLTEXF_IMPORTANTBITS (0)
#define GLTEXF_DESTROYED 0x00040000
#define GLTEXF_DYNAMIC 0x00080000

typedef struct textypeinfo_s
{
	textype_t textype;
	int inputbytesperpixel;
	int internalbytesperpixel;
	float glinternalbyteperpixel;
	int glformat;
	int glinternalformat;
	int gltype;
}
textypeinfo_t;

static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_BGRA, 3, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_BGRA, 4, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_palette_compress = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_BGRA, GL_COMPRESSED_RGB_ARB, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1.0f, GL_BGRA, GL_COMPRESSED_RGBA_ARB, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_rgba = {TEXTYPE_RGBA, 4, 4, 4.0f, GL_RGBA, 3, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA, 4, 4, 4.0f, GL_RGBA, 4, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_rgba_compress = {TEXTYPE_RGBA, 4, 4, 0.5f, GL_RGBA, GL_COMPRESSED_RGB_ARB, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_rgba_alpha_compress = {TEXTYPE_RGBA, 4, 4, 1.0f, GL_RGBA, GL_COMPRESSED_RGBA_ARB, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_bgra = {TEXTYPE_BGRA, 4, 4, 4.0f, GL_BGRA, 3, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_bgra_alpha = {TEXTYPE_BGRA, 4, 4, 4.0f, GL_BGRA, 4, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_bgra_compress = {TEXTYPE_BGRA, 4, 4, 0.5f, GL_BGRA, GL_COMPRESSED_RGB_ARB, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_bgra_alpha_compress = {TEXTYPE_BGRA, 4, 4, 1.0f, GL_BGRA, GL_COMPRESSED_RGBA_ARB, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_shadowmap = {TEXTYPE_SHADOWMAP, 4, 4, 4.0f, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT24_ARB, GL_UNSIGNED_BYTE};
static textypeinfo_t textype_projection = {TEXTYPE_PROJECTION, 4, 4, 4.0f, GL_LUMINANCE_ALPHA, GL_LUMINANCE16_ALPHA16, GL_UNSIGNED_BYTE};

typedef enum gltexturetype_e
{
	
}
gltexturetype_t;
